package com.authenticateCenter.controller;

import com.authenticateCenter.assembler.UserAssembler;
import com.authenticateCenter.domain.dto.UserDto;
import com.authenticateCenter.domain.entity.User;
import com.authenticateCenter.service.IUserRoleService;
import com.authenticateCenter.service.IUserService;
import com.authenticateCenter.domain.vo.UserVo;
import com.common.mybaits.CustomPage;
import com.common.result.ResponseResultAdvice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

import static com.authenticateCenter.factory.UserFactory.USER_CONVERT;

/**
 * @author 大忽悠
 * @create 2022/9/15 11:09
 */
@Validated
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "用户管理")
@ResponseResultAdvice
public class UserController {
     private final IUserService iUserService;
     private final UserAssembler userAssembler;
     private final IUserRoleService iUserRoleService;

     @ApiOperation("新增用户")
     @PostMapping("/add")
     public Integer addUser(@RequestBody@Valid UserDto userDto){
          User user = USER_CONVERT.toUser(userDto);
          user.insertCheck(iUserService);
          iUserService.save(user);
          return user.getId();
     }

     @ApiOperation("修改用户名")
     @PostMapping("/updateName")
     public void updateUserName(@RequestParam("oldUsername")String oldUsername,@RequestParam("newUsername")String newUsername){
          iUserService.updateName(oldUsername,newUsername);
     }

     @ApiOperation("启用用户")
     @PostMapping("/enable")
     public void enableUser(@RequestParam("id")Integer uid){
          iUserService.enableOrDisableUser(uid,Boolean.TRUE);
     }

     @ApiOperation("禁止用户")
     @PostMapping("/disable")
     public void disableUser(@RequestParam("id")Integer uid){
          iUserService.enableOrDisableUser(uid,Boolean.FALSE);
     }

     @ApiOperation("删除用户")
     @DeleteMapping("/delete")
     public void deleteUser(@RequestParam("id")Integer uid){
          iUserService.removeUser(uid,iUserRoleService);
     }

     @ApiOperation("根据用户名删除用户")
     @DeleteMapping("/delByName/{username}")
     public void deleteUserByName(@PathVariable("username")String username){
          iUserService.delByName(username);
     }

     @ApiOperation("查询所有用户")
     @GetMapping("/list")
     public List<UserVo> listUser(CustomPage customPage){
          return userAssembler.assemblers(customPage,null);
     }
}
